# -*- coding: utf-8 -*-
# ----------------------------
# @Time    : 2022/6/18 3:57 PM
# @Author  : changqingai
# @FileName: tf_norm.py
# ----------------------------

import tensorflow as tf

# ******* l2范式 **************
a = tf.random.normal([2, 2], stddev=1, mean=0)
b = tf.norm(a, ord=2)
c = tf.math.sqrt(tf.reduce_sum(tf.math.square(a)))
print("b:", b.numpy())
print("c:", c.numpy())

a = tf.random.normal([2, 2], stddev=1, mean=0)
b = tf.norm(a, ord=2, axis=1)
c = tf.math.sqrt(tf.reduce_sum(tf.math.square(a), axis=1))
print("b:", b.numpy())
print("c:", c.numpy())

# ******* l1范式 **************
a = tf.random.normal([2, 3], stddev=1, mean=0)
b = tf.norm(a, ord=1)
c = tf.math.reduce_sum(tf.math.abs(a))
print("b:", b.numpy())
print("c:", c.numpy())

a = tf.random.normal([2, 3], stddev=1, mean=0)
b = tf.norm(a, ord=1, axis=1)
c = tf.math.reduce_sum(tf.math.abs(a), axis=1)
print("b:", b.numpy())
print("c:", c.numpy())
